package sv.com.seguros.dao.asg;

import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

import sv.com.seguros.dao.BaseDaoImpl;
import sv.com.seguros.dto.asg.AsgPoliza;
import sv.com.seguros.exception.WebException;
import sv.com.seguros.web.util.WebUtilidades;

public class AsgPolizaDaoImpl extends BaseDaoImpl implements AsgPolizaDao {

	@SuppressWarnings("unchecked")
	public List<Object[]> findAllByAsgClienteId(Long asgClienteId){
		try {
			DetachedCriteria criteria = DetachedCriteria.forClass(AsgPoliza.class);
			criteria.add(Restrictions.eq("asgCliente.asgClienteId", asgClienteId));
			criteria.addOrder(Order.asc("asgPolizaNumero"));
			criteria.addOrder(Order.asc("asgPolizaFechaCreacion"));
			criteria.createAlias("ctgTipoSeguro", "ctgTipoSeguro");
			criteria.createAlias("ctgPlan", "ctgPlan");
			criteria.createAlias("ctgAsesor", "ctgAsesor");
			criteria.createAlias("ctgFormaPago", "ctgFormaPago");
			criteria.createAlias("ctgAseguradora", "ctgAseguradora");
			criteria.setProjection(Projections.projectionList().
				add(Projections.property("asgPolizaId")).
				add(Projections.property("asgPolizaNumero")).
				add(Projections.property("asgPolizaSumaAsegurada")).
				add(Projections.property("asgPolizaPrima")).
				add(Projections.property("asgPolizaCantidadCuota")).
				add(Projections.property("asgPolizaFechaInicio")).
				add(Projections.property("asgPolizaFechaFinalizacion")).
				add(Projections.property("ctgAseguradora.ctgAseguradoraId")).
				add(Projections.property("ctgAseguradora.ctgAseguradoraNombre")).
				add(Projections.property("ctgTipoSeguro.ctgCatalogoId")).
				add(Projections.property("ctgTipoSeguro.ctgCatalogoNombre")).
				add(Projections.property("ctgPlan.ctgPlanId")).
				add(Projections.property("ctgPlan.ctgPlanNombre")).
				add(Projections.property("ctgAsesor.ctgAsesorId")).
				add(Projections.property("ctgAsesor.ctgAsesorNombreCompleto")).
				add(Projections.property("ctgFormaPago.ctgCatalogoId")).
				add(Projections.property("ctgFormaPago.ctgCatalogoNombre"))
			);
			return (List<Object[]>) findByCriteria(criteria);
		} catch (Exception e) {
			logger.error("Exception:", e);
			e.printStackTrace();
		}
		return Collections.emptyList();
	}

	public void cancelar(Long asgPolizaId) throws WebException{
		String hqlQuery = "update AsgPoliza set " +
				"asgPolizaFechaRenovacion = :asgPolizaFechaRenovacion, asgPolizaRenovada = '2' where asgPolizaId = :asgPolizaId";
		Map<String, Object> parameters = new HashMap<String, Object>();
		parameters.put("asgPolizaFechaRenovacion", WebUtilidades.dateFormatAsYYYYMMDDHHMMSS.format(new Date()));
		parameters.put("asgPolizaId", asgPolizaId);
		try{
			executeNonHibernateQuery(hqlQuery, parameters);
		}catch(Exception e){
			throw new WebException("Problemas a tratar de actualizar la informaci\u00F3n");
		}
	}
}
